use crate::{AppError, AppResult, DashboardData, User};
use crate::sip::SipService;
use crate::auth::AuthService;

#[tauri::command]
pub async fn get_dashboard_data() -> AppResult<DashboardData> {
    // 初始化示例数据
    AuthService::init_default_users();
    SipService::init_sample_data();
    
    // 获取用户统计
    let total_users = get_total_users_count();
    let online_users = get_online_users_count();
    
    // 获取通话统计
    let total_calls = SipService::get_calls().len() as i64;
    let active_calls = SipService::get_active_calls_count();
    
    // 获取系统统计
    let system_stats = crate::utils::Utils::get_system_stats();
    
    // 获取最近通话记录
    let recent_calls = get_recent_calls();
    
    // 获取最近用户
    let recent_users = get_recent_users();
    
    Ok(DashboardData {
        total_users,
        online_users,
        total_calls,
        active_calls,
        system_stats,
        recent_calls,
        recent_users,
    })
}

// 获取用户总数
fn get_total_users_count() -> i64 {
    // 这里应该从数据库获取，目前返回模拟数据
    15
}

// 获取在线用户数
fn get_online_users_count() -> i64 {
    // 这里应该从数据库获取，目前返回模拟数据
    3
}

// 获取最近通话记录
fn get_recent_calls() -> Vec<crate::Call> {
    let mut calls = SipService::get_calls();
    
    // 按开始时间排序，取最近5条
    calls.sort_by(|a, b| b.start_time.cmp(&a.start_time));
    calls.truncate(5);
    
    calls
}

// 获取最近用户
fn get_recent_users() -> Vec<User> {
    // 这里应该从数据库获取，目前返回模拟数据
    vec![
        User {
            id: "user_001".to_string(),
            username: "zhangsan".to_string(),
            nickname: "张三".to_string(),
            email: Some("zhangsan@example.com".to_string()),
            phone: Some("13800138001".to_string()),
            avatar: None,
            status: crate::UserStatus::Active,
            role_ids: vec!["user".to_string()],
            dept_id: Some("1".to_string()),
            dept_name: Some("技术部".to_string()),
            create_time: chrono::Utc::now() - chrono::Duration::days(30),
            update_time: chrono::Utc::now() - chrono::Duration::days(5),
            last_login_time: Some(chrono::Utc::now() - chrono::Duration::hours(2)),
        },
        User {
            id: "user_002".to_string(),
            username: "lisi".to_string(),
            nickname: "李四".to_string(),
            email: Some("lisi@example.com".to_string()),
            phone: Some("13800138002".to_string()),
            avatar: None,
            status: crate::UserStatus::Active,
            role_ids: vec!["user".to_string()],
            dept_id: Some("2".to_string()),
            dept_name: Some("销售部".to_string()),
            create_time: chrono::Utc::now() - chrono::Duration::days(25),
            update_time: chrono::Utc::now() - chrono::Duration::days(3),
            last_login_time: Some(chrono::Utc::now() - chrono::Duration::hours(5)),
        },
        User {
            id: "user_003".to_string(),
            username: "wangwu".to_string(),
            nickname: "王五".to_string(),
            email: Some("wangwu@example.com".to_string()),
            phone: Some("13800138003".to_string()),
            avatar: None,
            status: crate::UserStatus::Inactive,
            role_ids: vec!["user".to_string()],
            dept_id: Some("1".to_string()),
            dept_name: Some("技术部".to_string()),
            create_time: chrono::Utc::now() - chrono::Duration::days(20),
            update_time: chrono::Utc::now() - chrono::Duration::days(1),
            last_login_time: Some(chrono::Utc::now() - chrono::Duration::days(2)),
        },
    ]
}

#[tauri::command]
pub async fn get_system_info() -> AppResult<std::collections::HashMap<String, String>> {
    Ok(crate::utils::Utils::get_system_info())
}

#[tauri::command]
pub async fn get_dashboard_call_statistics() -> AppResult<std::collections::HashMap<String, i64>> {
    Ok(SipService::get_call_statistics())
}

#[tauri::command]
pub async fn get_user_statistics() -> AppResult<std::collections::HashMap<String, i64>> {
    let mut stats = std::collections::HashMap::new();
    
    // 模拟用户统计数据
    stats.insert("total".to_string(), 15);
    stats.insert("active".to_string(), 12);
    stats.insert("inactive".to_string(), 3);
    stats.insert("online".to_string(), 3);
    
    Ok(stats)
}

#[tauri::command]
pub async fn get_sip_statistics() -> AppResult<std::collections::HashMap<String, i64>> {
    let mut stats = std::collections::HashMap::new();
    
    // 获取SIP相关统计数据
    stats.insert("total_calls".to_string(), SipService::get_calls().len() as i64);
    stats.insert("active_calls".to_string(), SipService::get_active_calls_count());
    stats.insert("registered_users".to_string(), SipService::get_registered_users_count());
    stats.insert("total_registrations".to_string(), SipService::get_registers().len() as i64);
    
    Ok(stats)
}
